const db = require('./src/config/database');
const Instruction = require('./src/modules/instruction/Instruction');

// 插入真实的货物数据
function insertRealGoods() {
    const realGoods = [
        // 原材料类货物
        { name: '原材料A', sku: 'RM001', quantity: 100, location: 'WH01-A-01-01-01', status: 'inStock', batch_number: 'BN001', specification: 'Φ10mm', unit: '公斤' },
        { name: '原材料A', sku: 'RM001', quantity: 50, location: 'WH01-A-01-01-02', status: 'inStock', batch_number: 'BN006', specification: 'Φ10mm', unit: '公斤' }, // 同种货物不同货位
        { name: '原材料B', sku: 'RM002', quantity: 200, location: 'WH01-A-01-01-03', status: 'inStock', batch_number: 'BN002', specification: 'Φ15mm', unit: '公斤' },
        
        // 半成品类货物
        { name: '半成品C', sku: 'SF001', quantity: 150, location: 'WH01-B-02-02-01', status: 'inStock', batch_number: 'BN003', specification: '标准件', unit: '件' },
        { name: '半成品C', sku: 'SF001', quantity: 80, location: 'WH01-B-02-02-02', status: 'inStock', batch_number: 'BN007', specification: '标准件', unit: '件' }, // 同种货物不同货位
        
        // 成品类货物
        { name: '成品D', sku: 'FP001', quantity: 80, location: 'WH01-C-03-03-01', status: 'inStock', batch_number: 'BN004', specification: '定制件', unit: '套' },
        
        // 包装材料类货物
        { name: '包装材料E', sku: 'PM001', quantity: 300, location: 'WH01-D-01-01-01', status: 'inStock', batch_number: 'BN005', specification: '500x300x200mm', unit: '个' },
        { name: '包装材料F', sku: 'PM002', quantity: 150, location: 'WH01-D-01-01-02', status: 'inStock', batch_number: 'BN008', specification: '400x200x150mm', unit: '个' }
    ];

    realGoods.forEach(goods => {
        const sql = `INSERT OR IGNORE INTO goods (name, sku, quantity, location, status, batch_number, specification, unit) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
        db.run(sql, [goods.name, goods.sku, goods.quantity, goods.location, goods.status, goods.batch_number, goods.specification, goods.unit], (err) => {
            if (err) {
                console.error('插入货物数据失败:', err.message);
            } else {
                console.log(`货物数据已插入: ${goods.name} (${goods.location})`);
            }
        });
    });
}

// 插入真实的入库单据数据
function insertRealInboundDocuments() {
    const realInboundDocs = [
        { goods_id: 1, supplier: '供应商甲', quantity: 50, expected_arrival_date: '2025-11-10', status: 'pending' },
        { goods_id: 2, supplier: '供应商甲', quantity: 30, expected_arrival_date: '2025-11-10', status: 'pending' }, // 同一供应商的另一种货物
        { goods_id: 3, supplier: '供应商乙', quantity: 100, expected_arrival_date: '2025-11-12', status: 'received' },
        { goods_id: 4, supplier: '供应商丙', quantity: 75, expected_arrival_date: '2025-11-15', status: 'completed' },
        { goods_id: 5, supplier: '供应商丙', quantity: 40, expected_arrival_date: '2025-11-15', status: 'completed' } // 同一供应商的另一种货物
    ];

    realInboundDocs.forEach(doc => {
        const sql = `INSERT OR IGNORE INTO inbound_documents (goods_id, supplier, quantity, expected_arrival_date, status) VALUES (?, ?, ?, ?, ?)`;
        db.run(sql, [doc.goods_id, doc.supplier, doc.quantity, doc.expected_arrival_date, doc.status], (err) => {
            if (err) {
                console.error('插入入库单据数据失败:', err.message);
            } else {
                console.log(`入库单据数据已插入: ${doc.supplier}`);
            }
        });
    });
}

// 插入真实的出库单据数据
function insertRealOutboundDocuments() {
    const realOutboundDocs = [
        { goods_id: 6, customer: '客户甲', quantity: 30, expected_pickup_date: '2025-11-08', status: 'pending' },
        { goods_id: 1, customer: '客户乙', quantity: 20, expected_pickup_date: '2025-11-10', status: 'picked' },
        { goods_id: 7, customer: '客户丙', quantity: 50, expected_pickup_date: '2025-11-13', status: 'completed' },
        { goods_id: 2, customer: '客户乙', quantity: 15, expected_pickup_date: '2025-11-10', status: 'picked' } // 同一客户的另一种货物
    ];

    realOutboundDocs.forEach(doc => {
        const sql = `INSERT OR IGNORE INTO outbound_documents (goods_id, customer, quantity, expected_pickup_date, status) VALUES (?, ?, ?, ?, ?)`;
        db.run(sql, [doc.goods_id, doc.customer, doc.quantity, doc.expected_pickup_date, doc.status], (err) => {
            if (err) {
                console.error('插入出库单据数据失败:', err.message);
            } else {
                console.log(`出库单据数据已插入: ${doc.customer}`);
            }
        });
    });
}

// 插入真实的指令数据
function insertRealInstructions() {
    const realInstructions = [
        // 当前指令（未下发、已下发、正在执行）
        { 
            taskId: 'TASK001', 
            taskType: '入库', 
            palletId: 'P001', 
            startLocation: '入库口-1', 
            targetLocation: 'WH01-A-01-01-01', 
            startDevice: '输送线1', 
            endDevice: '堆垛机1', 
            materialCode: 'RM001', 
            materialName: '原材料A', 
            executeQuantity: 50, 
            status: 'not_issued' 
        },
        { 
            taskId: 'TASK002', 
            taskType: '出库', 
            palletId: 'P002', 
            startLocation: 'WH01-B-02-02-01', 
            targetLocation: '出库口-1', 
            startDevice: '堆垛机2', 
            endDevice: '输送线2', 
            materialCode: 'SF001', 
            materialName: '半成品C', 
            executeQuantity: 30, 
            status: 'issued' 
        },
        { 
            taskId: 'TASK003', 
            taskType: '移库', 
            palletId: 'P003', 
            startLocation: 'WH01-C-03-03-01', 
            targetLocation: 'WH01-C-03-03-02', 
            startDevice: '堆垛机3', 
            endDevice: '堆垛机3', 
            materialCode: 'FP001', 
            materialName: '成品D', 
            executeQuantity: 20, 
            status: 'executing' 
        },
        { 
            taskId: 'TASK006', 
            taskType: '入库', 
            palletId: 'P001', 
            startLocation: '入库口-1', 
            targetLocation: 'WH01-A-01-01-04', 
            startDevice: '输送线1', 
            endDevice: '堆垛机1', 
            materialCode: 'RM002', 
            materialName: '原材料B', 
            executeQuantity: 40, 
            status: 'not_issued' 
        }, // 同一托盘上不同货物
        { 
            taskId: 'TASK007', 
            taskType: '移库', 
            palletId: 'P004', 
            startLocation: 'WH01-D-01-01-01', 
            targetLocation: 'WH01-D-01-01-03', 
            startDevice: '堆垛机4', 
            endDevice: '堆垛机4', 
            materialCode: 'PM001', 
            materialName: '包装材料E', 
            executeQuantity: 100, 
            status: 'issued' 
        },
        // 历史指令（已完成、已撤销）
        { 
            taskId: 'TASK004', 
            taskType: '入库', 
            palletId: 'P004', 
            startLocation: '入库口-2', 
            targetLocation: 'WH01-A-01-01-03', 
            startDevice: '输送线1', 
            endDevice: '堆垛机1', 
            materialCode: 'RM002', 
            materialName: '原材料B', 
            executeQuantity: 100, 
            status: 'completed' 
        },
        { 
            taskId: 'TASK005', 
            taskType: '出库', 
            palletId: 'P005', 
            startLocation: 'WH01-D-01-01-02', 
            targetLocation: '出库口-2', 
            startDevice: '堆垛机4', 
            endDevice: '输送线3', 
            materialCode: 'PM002', 
            materialName: '包装材料F', 
            executeQuantity: 50, 
            status: 'cancelled' 
        }
    ];

    realInstructions.forEach(instructionData => {
        const instruction = new Instruction(
            null,
            instructionData.taskId,
            instructionData.taskType,
            instructionData.palletId,
            instructionData.startLocation,
            instructionData.targetLocation,
            instructionData.startDevice,
            instructionData.endDevice,
            instructionData.materialCode,
            instructionData.materialName,
            instructionData.executeQuantity,
            instructionData.status,
            null,
            null
        );

        instruction.save((err, result) => {
            if (err) {
                console.error('插入指令数据失败:', err.message);
            } else {
                console.log(`指令数据已插入: ${instructionData.taskId}`);
            }
        });
    });
}

// 执行数据插入
console.log('开始插入真实数据...');
insertRealGoods();
insertRealInboundDocuments();
insertRealOutboundDocuments();
insertRealInstructions();
console.log('真实数据插入完成。');